package baseStructure;

/***
 * @author Ragnvald Fuglestein
 */

import java.io.Serializable;

public final class Track extends Base implements Serializable {
	private static final long serialVersionUID = -965042067871967101L;
	private Artist artist;
	private Album album;
	
	public int num = -1;

	public Track(final Artist artist, final Album album, final String name) {
		super(name);
		this.artist = artist;
		this.album = album;
	}
	
	public Track(final Artist artist, final Album album, final String name, final int num) {
		super(name);
		this.artist = artist;
		this.album = album;
		setNum(num);
	}

	public Track(final Artist artist, final String name) {
		super(name);
		this.artist = artist;
		this.album = null;
	}
	
	public Track(final Artist artist, final String name, final int num) {
		super(name);
		this.artist = artist;
		this.album = null;
		setNum(num);
	}

	public Track(final String name) {
		super(name);
		this.artist = null;
		this.album = null;
	}
	
	public Track(final String name, final int num) {
		super(name);
		this.artist = null;
		this.album = null;
		setNum(num);
	}
	
	/***
	 * 
	 * @return associated artist object or null
	 */
	public final Artist getArtist() {
		return artist;
	}
	
	public final void setArtist(Artist artist) {
		this.artist = artist;
	}
	
	/***
	 * 
	 * @return associated album object or null
	 */
	public final Album getAlbum() {
		return album;
	}

	public final void setAlbum(Album album) {
		this.album = album;
	}
	
	/***
	 * 
	 * @return tracknum or -1 if not set
	 */
	public final int getNum() {
		return num;
	}
	
	/***
	 * 
	 * @return tracknum or "" if not set
	 */
	public final String getNumStr() {
		if (num < 0) return "";
		
		if (num < 10) return "0" + num;
		return "" + num;
	}
	
	public final void setNum(final int num) {
		this.num = num;
		notifyElementChangedListener();
	}
	
	/***
	 * Merge tracknum
	 * @param track
	 * @return result of base.merge()
	 */
	public final boolean merge(final Track track) {
		int tn;
		if ((tn = track.getNum()) != -1) setNum(tn);
		return super.merge(track);
	}

	/***
	 * 
	 * @return SearchTrack with name and num if set
	 */
	public final SearchTrack toSearch() {
		if (num != -1 ) return new SearchTrack(getName(), num);
		return new SearchTrack(getName());
	}
	
	public String toString() {
		if (num == -1) return getName();
		else if (num <= 9) return "0" + num + ". " + getName();
		return num + ". " + getName();
	}
	
	public String getInfo() {
		String ret = "";
		
		ret += "Artist name: " + getArtist().getName() + "\n";
		if (album != null) ret += "Album name: " + getAlbum().getName() + "\n";
		ret += "Track name: " + getName() + "\n";
		if (num != -1) ret += "Track number: " + getNumStr() + "\n";
		ret += super.getInfo();
		
		return ret;
	}
}
